<!--计划物料配置-->
<template>
	<div class="m-view-companyInfo m-window">
		<div class="m-condition">
			<el-row :gutter="10">
				<el-form
					:model="haderRuleForm"
					ref="haderRuleForm"
					label-width="100px"
					:inline-message="true"
					label-position="left"
				>
					<el-col :span="6">
						<el-form-item label="物料编号" class="rightItem">
							<el-input v-model="matCode" placeholder="请输入物料编号" clearable @keyup.enter.native="search"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="物料描述" class="rightItem">
							<el-input v-model="matName" placeholder="请输入物料描述" clearable @keyup.enter.native="search"></el-input>
						</el-form-item>
					</el-col>
				</el-form>
				<el-col :span="2" :offset="4">
					<el-button type="primary" @click="search">查询</el-button>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" @click="add">新增</el-button>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" @click="del">删除</el-button>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" @click="importInfo">导入</el-button>
				</el-col>
			</el-row>
		</div>
		<div class="m-agTable m-bottom">
			<agGridComponents
				ref="agGridComponent"
				:getGridOptions="gridOptions"
				:getColumnDefs="columnDefs"
				:isColFit="true"
				:getRowSelection="'multiple'"
			></agGridComponents>
		</div>
		<div class="m-page">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				@prev-click="handlePrevClick"
				@next-click="handleNextClick"
				:current-page="currentPage"
				:page-sizes="[30,50,100,150,200]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
			></el-pagination>
		</div>
		<!-- 新增物料弹窗 -->
		<el-dialog
			title="新增"
			v-if="dialogVisible"
			:visible.sync="dialogVisible"
			width="50%"
			:before-close="handleClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<el-form
				:model="ruleForm"
				ref="ruleForm"
				label-width="100px"
				class="demo-ruleForm"
				:rules="rules"
			>
				<el-form-item label="物料编号" prop="matCode">
					<el-input
						v-model="ruleForm.matCode"
						placeholder="请输入10位物料编号"
						clearable
						maxlength="10"
						@keyup.native="check"
						@blur="matCodeBlur"
						@keyup.enter.native="matCodeBlur"
						:disabled="inputDis"
					></el-input>
				</el-form-item>
				<el-form-item label="物料描述" prop="matName">
					<el-input v-model="ruleForm.matName" placeholder="物料描述" disabled clearable maxlength="10"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="saveData" :disabled.sync="disState">保存</el-button>
				<el-button type="primary" @click="saveDataAgain" :disabled.sync="disState">保存并继续</el-button>
			</span>
		</el-dialog>
		<!-- 导入物料弹窗 -->
		<el-dialog
			title="导入模板"
			v-if="importDialogVisible"
			:visible.sync="importDialogVisible"
			width="50%"
			:before-close="importHandleClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<importFile
				:moduleName="moduleName"
				@setImportData="getImportData"
				:closeImportDialog="importDialogVisible"
				:tempUrl="tempUrl"
			></importFile>
			<span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="saveImportData">导入</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
import _Api from "@/api"
import localeText from "@/i18n/agGridText"
import agGridComponents from "@/components/agGridComponents"
import importFile from "@/components/importFile"
import fileStream from "@/public/fileStream"

export default {
	data () {
		return {
			matCode: "",
			matName: "",
			currentPage: 1,
			pageSize: 30,
			total: 0,
			gridOptions: "",
			columnDefs: "",
			dialogVisible: false,
			ruleForm: {
				matCode: '',
				matName: ''
			},
			haderRuleForm: {},
			rules: {
				matCode: [{
					required: true,
					message: "请输入10位物料编号"
				},
				{
					min: 10,
					max: 10,
					trigger: "change",
					message: "请输入10位物料编号"
				}],
				matName: [{
					required: true,
					message: "请输入正确的物料编号获取物料描述"
				}]
			},
			disState: false,
			rowData: [],
			current: 1,
			inputDis: false,
			importDialogVisible: false,
			moduleName: "materialAntiCounterfeition",
			importData: [],
			closeImportDialog: false,
			//   物料导入模板下载地址
			tempUrl: ''
		}
	},
	beforeMount () {
		this.gridOptions = {
			context: {
				componentParent: this
			},
			enableColResize: true,
			localeText: localeText,
			defaultColDef: {
				menuTabs: []
			},
			suppressRowClickSelection: true//单元格选中,false为选中整行
		};
		this.columnDefs = [
			{ headerName: "物料编号", field: "matCode", checkboxSelection: true, width: 140 },
			{ headerName: "物料描述", field: "matName", width: 260, tooltipField: 'matName' }
		]
	},
	methods: {
		importHandleClose (done) {
			done();
			this.importData = []
			this.importDialogVisible = false;
		},
		// 保存导入的物料信息
		saveImportData () {
			if (Array.isArray(this.importData) && this.importData.length < 1) {
				this.$alert("提示：请导入相关数据！", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
			for (let i in this.importData) {
				if (!this.importData[i].matName) {
					this.$alert("物料描述不能为空,请检查物料编码是否正确!", {
						showClose: false,
						confirmButtonText: "确定",
						customClass: "alertPrompt"
					});
					return;
				} else if (!this.importData[i].matCode) {
					this.$alert("物料编码不能为空!", {
						showClose: false,
						confirmButtonText: "确定",
						customClass: "alertPrompt"
					});
					return;
				}
				this.importData[i].creator = this.$global.user.employeeName;
				this.importData[i].creatorId = this.$global.user.id;
			}
			_Api.batchSavePlanMaterial(this.importData).then((response) => {
				let data = response.data;
				this.importDialogVisible = false;
				this.importData = []
				if (data.code == "1") {
					this.$message({
						message: "导入成功",
						type: "success"
					})
					this.search()
				} else {
					this.$alert(data.info, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
				}
			}).catch((err) => {
				this.importData = []
				this.$alert(err.data.message, {
					showClose: false,
					confirmButton: "确定",
					customClass: "alertFailure"
				});
			})
		},
		// 获取导入的物料数据
		getImportData (params) {
			this.importData = params;
		},
		// 查询计划物料列表
		search () {
			let obj = {}
			obj.likeParams = {}
			if (this.matCode != "")
				obj.likeParams.matCode = this.matCode
			if (this.matName != "")
				obj.likeParams.matName = this.matName
			if (this.matCode || this.matName) {
				this.currentPage = 1
				this.pageSize = 30
				obj.current = 1
				obj.size = 30
			} else {
				obj.current = this.currentPage
				obj.size = this.pageSize
			}
			this.$refs.agGridComponent.loading = true
			_Api.getPlanMaterialList(obj).then((response) => {
				let data = response.data;
				this.$refs.agGridComponent.loading = false
				if (data.code == "1") {
					if (data.total > 0) {
						this.rowData = data.rows.splice(0)
						this.$refs.agGridComponent.gridOptions.api.setRowData(this.rowData)
						this.total = data.total
					} else {
						this.$refs.agGridComponent.gridOptions.api.setRowData([])
						this.total = 0
						this.current = 1
						this.currentPage = 1
					}
				} else {
					this.$alert(data.info, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					})
					this.$refs.agGridComponent.gridOptions.api.setRowData([])
					this.total = 0
					this.current = 1
					this.currentPage = 1
				}
			}).catch(() => {
				this.$alert("操作失败", {
					showClose: false,
					confirmButton: "确定",
					customClass: "alertFailure"
				})
				this.$refs.agGridComponent.loading = false
			})
		},
		// 打开新增弹窗
		add () {
			this.ruleForm = {
				matCode: '',
				matName: ''
			}
			this.dialogVisible = true;
			this.disState = true;
		},
		// 批量删除物料
		del () {
			let row = this.$refs.agGridComponent.gridOptions.api.getSelectedRows();
			if (row.length <= 0) {
				this.$alert("提示：请选择需要删除的数据", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
			this.$confirm("是否确认删除该数据？", "消息提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				distinguishCancelAndClose: true
			}).then(() => {
				let ids = row.map(item => {
					return item.id
				});
				_Api.deletedPlanMaterial(ids).then((response) => {
					let data = response.data;
					if (data.code == "1") {
						this.$message({
							message: "操作成功",
							type: "success"
						});
					} else {
						this.$alert(data.info, {
							showClose: false,
							confirmButton: "确定",
							customClass: "alertFailure"
						});
					}
					this.search()
				}).catch((err) => {
					this.$alert(err.data.message, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
				})
			}).catch(() => { })
		},
		importInfo () {
			this.importDialogVisible = true;
		},
		handleSizeChange (params) {
			this.currentPage = 1;
			this.pageSize = params;
			this.search();
		},
		handleCurrentChange (params) {
			this.currentPage = params;
			this.search();
		},
		handlePrevClick (params) {
			this.currentPage = params;
			this.search();
		},
		handleNextClick (params) {
			this.currentPage = params;
			this.search();
		},
		handleClose (done) {
			this.$refs.ruleForm.resetFields();
			this.$refs.ruleForm.clearValidate();
			done()
		},
		cancel () {
			this.$refs.ruleForm.resetFields();
			this.$refs.ruleForm.clearValidate();
			this.dialogVisible = false;
		},
		saveData () {
			this.$refs["ruleForm"].validate((valid) => {
				if (valid) {
					this.ruleForm.creator = this.$global.user.loginName;
					this.ruleForm.creatorId = this.$global.user.id;
					this.disState = true;
					_Api.savePlanMaterial(this.ruleForm).then((response) => {
						let data = response.data;
						if (data.code == "1") {
							this.$message({
								message: "保存成功",
								type: "success"
							});
							this.ruleForm = {
								matCode: '',
								matName: ''
							};
							this.dialogVisible = false;
							this.search();
						} else {
							this.$alert(data.info, {
								showClose: false,
								confirmButton: "确定",
								customClass: "alertFailure"
							});
						}
						this.disState = false;
					}).catch((err) => {
						this.$alert(err.data.message, {
							showClose: false,
							confirmButton: "确定",
							customClass: "alertFailure"
						});
						this.disState = false;
					})
				} else {
					return false;
				}
			});
		},
		// 保存并继续
		saveDataAgain () {
			if (this.ruleForm.isBarCode == true) {
				this.ruleForm.isBarCode = 1
			} else {
				this.ruleForm.isBarCode = 0
			}
			this.$refs["ruleForm"].validate((valid) => {
				if (valid) {
					this.ruleForm.creator = this.$global.user.loginName;
					this.ruleForm.creatorId = this.$global.user.id;
					this.disState = true;
					_Api.savePlanMaterial(this.ruleForm).then((response) => {
						let data = response.data;
						if (data.code == "1") {
							this.$message({
								message: "保存成功",
								type: "success"
							});
							this.ruleForm = {
								matCode: '',
								matName: ''
							}
							this.search()
						} else {
							this.$alert(data.info, {
								showClose: false,
								confirmButton: "确定",
								customClass: "alertFailure"
							});
						}
						this.disState = false;
					}).catch((err) => {
						this.$alert(err.data.message, {
							showClose: false,
							confirmButton: "确定",
							customClass: "alertFailure"
						});
						this.disState = false;
					})
				} else {
					return false;
				}
			})
		},
		check () {
			this.ruleForm.matCode = this.ruleForm.matCode.replace(/[^\.\d]/g, '');
			if (/\d/.test(this.ruleForm.matCode))
				this.disState = false;
			else
				this.disState = true;
		},
		// 获取sap物料信息
		matCodeBlur () {
			let that = this;
			if (this.ruleForm.matCode) {
				if (this.ruleForm.matCode.toString().length >= 10) {
					this.inputDis = true;
					let obj = {
						matCode: this.ruleForm.matCode
					}
					_Api.getMatName(obj).then(function (response) {
						that.inputDis = false;
						let data = response.data;
						if (data.code == "1") {
							// that.ruleForm.matName = data.rows;
							that.$set(that.ruleForm, "matName", data.rows);
							that.$refs.ruleForm.clearValidate();
						} else {
							that.$alert(data.info, {
								showClose: false,
								confirmButton: "确定",
								customClass: "alertFailure"
							});
						}
					}).catch(() => {
						that.inputDis = false;
						that.$alert("操作失败", {
							showClose: false,
							confirmButton: "确定",
							customClass: "alertFailure"
						});
					})
				}
			}
		},
		// 获取合同导入模板
		getContractTemp () {
			fileStream.getPrintTempUrl("materialImport").then(url => {
				this.tempUrl = url
			})
		}
	},
	components: {
		agGridComponents,
		importFile
	},
	mounted () {
		this.getContractTemp()
		this.search()
	}
}
</script>